﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace nhahangbarota
{
    public partial class MonAn : Form
    {
        DataTable dt = new DataTable();
        public MonAn()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            Formchinh a = new Formchinh();
            a.ShowDialog();
        }

        private void MonAn_Load(object sender, EventArgs e)
        {
            MonAnBUS ma = new MonAnBUS();
            dt = ma.LoadDS();

            cbxLoaiMon.DataSource = dt;
            cbxLoaiMon.DisplayMember = "TenLoaiMon";
            cbxLoaiMon.ValueMember = "MaLoaiMon";

            int maMon = int.Parse(cbxLoaiMon.SelectedValue.ToString());
            dt = ma.LoadMonTheoMa(maMon);
            foreach (DataRow dtRow in dt.Rows)
            {
                if (dtRow[2].ToString() == "1")
                {
                    gvDSMonNuoc.Rows.Add(dtRow[0].ToString(), dtRow[1].ToString(), "Món Ăn", dtRow[3].ToString());
                }
                else
                {
                    gvDSMonNuoc.Rows.Add(dtRow[0].ToString(), dtRow[1].ToString(), "Món Uống", dtRow[3].ToString());
                }
            }
        }

        private void cbxLoaiMon_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cbxLoaiMon.SelectedIndex < 0)
                return;

            gvDSMonNuoc.Rows.Clear();
            int maMon = int.Parse(cbxLoaiMon.SelectedValue.ToString());
            MonAnBUS ma = new MonAnBUS();

            if (maMon != 3)
            {
                dt = ma.LoadMonTheoMa(maMon);

                foreach (DataRow dtRow in dt.Rows)
                {
                    if (dtRow[2].ToString() == "1")
                    {
                        gvDSMonNuoc.Rows.Add(dtRow[0].ToString(), dtRow[1].ToString(), "Món Ăn", dtRow[3].ToString());
                    }
                    else
                    {
                        gvDSMonNuoc.Rows.Add(dtRow[0].ToString(), dtRow[1].ToString(), "Món Uống", dtRow[3].ToString());
                    }
                }
            }
            else
            {
                dt = ma.LoadMenu();

                foreach (DataRow dtRow in dt.Rows)
                {
                    if (dtRow[2].ToString() == "1")
                    {
                        gvDSMonNuoc.Rows.Add(dtRow[0].ToString(), dtRow[1].ToString(), "Món Ăn", dtRow[3].ToString());
                    }
                    else
                    {
                        gvDSMonNuoc.Rows.Add(dtRow[0].ToString(), dtRow[1].ToString(), "Món Uống", dtRow[3].ToString());
                    }
                }
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            int co = 0;
            for (int i = 0; i < listMon.Count; i++)
            {
                MonAnBUS maBUS = new MonAnBUS();
                if (maBUS.CapNhat(listMon[i]) == false)
                    co = 1;
            }

            if (co == 0)
                MessageBox.Show("Cập nhật thành công","Thông báo");
            cbxLoaiMon_SelectionChangeCommitted(sender, e);
        }

        private void btnThemMonNuoc_Click(object sender, EventArgs e)
        {
            ThemMon f1 = new ThemMon();            
            f1.ShowDialog();
            cbxLoaiMon_SelectionChangeCommitted(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int index = gvDSMonNuoc.CurrentRow.Index;

            int maMon = int.Parse(gvDSMonNuoc.Rows[index].Cells[0].Value.ToString());

            MonAnBUS maBUS = new MonAnBUS();
            if (maBUS.XoaMon(maMon) == true)
            {
                MessageBox.Show("Xóa thành công !!");
                cbxLoaiMon_SelectionChangeCommitted(sender, e);
            }
        }
        List<MonAnDTO> listMon = new List<MonAnDTO>();
        private void gvDSMonNuoc_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = gvDSMonNuoc.CurrentRow.Index;

            string loai = gvDSMonNuoc.Rows[index].Cells[2].Value.ToString();

            MonAnDTO maDTO = new MonAnDTO();

            if (loai == "Món Ăn")
            {
                maDTO.Loai = 1;
            }
            else
            {
                maDTO.Loai = 2;
            }

            maDTO.MaMon = int.Parse(gvDSMonNuoc.Rows[index].Cells[0].Value.ToString());
            maDTO.TenMon = gvDSMonNuoc.Rows[index].Cells[1].Value.ToString();
            maDTO.Gia = float.Parse(gvDSMonNuoc.Rows[index].Cells[3].Value.ToString());
            
            listMon.Add(maDTO);
        }

    }
}
